const QQSound = require("QQSound");
const NativeSound = require("NativeSound");


/**音乐状态对应的值
 * cc.audioEngine.AudioState.ERROR： -1
 * cc.audioEngine.AudioState.INITIALZING ： 0
 * cc.audioEngine.AudioState.PLAYING ： 1
 * cc.audioEngine.AudioState.PAUSED ： 2
 */


window.SoundUtil = (function(){

    

    var _static = {
        isOpenMusic : true,
        isOpeneffect : true,
        playingMusicName : null,

        /**
         * 是否开启音乐
         * @param isOpenMusic
         */
        openMusic:function(isOpenMusic){
            this.isOpenMusic = isOpenMusic;
            if(!isOpenMusic){
                 this.pauseMusic();
            }else{
                 this.resumeMusic();
            }
        },

        /**
         * 是否开启音效
         * @param isOpenMusic
         */
        openEffect:function(isOpeneffect){
            this.isOpeneffect = isOpeneffect;
        },

        /**
         * url: 音乐文件路径
         * isAssets: 音乐文件是否存放在assets文件夹目录或子文件夹目录下。如果为true，目录必须以assets为根目录
         */
        playEffect : function (fileName) {
            if(!this.isOpeneffect){
                return;
            }
            if( fileName === ""){
                return;
            }
            if (SystemInfo.getPlatform() == 'qqPlay') {
                QQSound.playEffect(fileName);
            }else{
                NativeSound.playEffect(fileName);
            }
        },

        /**
         * 停止当前音乐
         */
        stopMusic : function () {
            if(!this.playingMusicName){
                return;
            }
            if (SystemInfo.getPlatform() == 'qqPlay') {
                QQSound.stopMusic();
            }else{
                NativeSound.stopMusic();
            }
            this.playingMusicName = null;
        },

        /**
         * 播放背景音乐
         * @param fileName
         */
        playMusic : function ( fileName,isLoop) {
            Log.debug("开始播放音乐",fileName)
                if(fileName == '' || this.playingMusicName == fileName || !this.isOpenMusic){
                    return;
                }
                this.stopMusic();
                if (SystemInfo.getPlatform() == 'qqPlay') {
                    QQSound.playMusic(fileName,isLoop);
                }else{
                    NativeSound.playMusic(fileName,isLoop);
                }
            this.playingMusicName = fileName;
            },

        /**
         * 恢复播放
         */
        resumeMusic:function(fileName){
            if(SystemInfo.getIsPlayingVideo()){
                return;
            }
                if (SystemInfo.getPlatform() == 'qqPlay') {
                    QQSound.resumeMusic(fileName);
                }else{
                    NativeSound.resumeMusic(fileName);
                }
            },

        /**
         * 暂停播放
         * @param fileName
         */
        pauseMusic: function(fileName){
            if (SystemInfo.getPlatform() == 'qqPlay') {
                QQSound.pauseMusic(fileName);
            }else{
                NativeSound.pauseMusic(fileName);
            }
        },

    }


    return _static;

})();

module.exports = SoundUtil;